(function(angular) {
	"use strict";
	angular.module("fs.waf").factory("payDetailEventService", payDetailEventService);
	payDetailEventService.$inject = [ "$injector", "$filter", "settlementInitFactory", "modalService", "payDetailService", "clsConstants" ];
	function payDetailEventService($injector, $filter, settlementInitFactory, modalService, payDetailService, clsConstants) {

		function paycodeChanged(settlement, payDetail, pageStatus, isTransfer, showMessage) {
			var isShowMessage = true;
			if (showMessage && showMessage == "no")
				isShowMessage = false;
			// TODO
			var payType = settlement.payment ? settlement.payment.repayType : "";
			var arr;
			if (payType == "C" || isTransfer) {
				arr = settlement.payCodeList;
			} else {
				arr = payDetailService.getSelectedPayCodeObjArr(settlement.payCodeList, payDetail.payCcy);
			}
			if (arr == null)
				return false;
			var payCodeCategory = "";
			for (var i = 0; i < arr.length; i++) {
				if (arr[i].paycode == payDetail.paycode && (arr[i].usedFor == payDetail.crDrFlag || arr[i].usedFor == "B")) {
					payCodeCategory = arr[i].paycodeCat;
					break;
				}
			}
			payDetail.payCodeCategory = payCodeCategory;
			payDetail.payCodeObj = payDetail.payCodeObj || {};
			payDetail.payCodeObj.paycodeCat = payCodeCategory;
			if (isShowMessage && payCodeCategory == "E") {
				var hintText = $filter("translate")("label.hint.paycodedropchanged");
				var message = new InfoMessage(hintText);
				message.show();
			}
			payDetailService.setPayDetailAcctInfo(settlement, payDetail, pageStatus, isTransfer);
			/*
			 * var itemType = payDetail.itemType; if(itemType=="P"){
			 * adjustmentRtgsAndSwiftForDetail(settlement, payDetail); }
			 */
			if (payDetailService.isUpdateChannelFee(settlement, payDetail)) {
				$injector.get("setlDetailService").updateSetDetailByFee(settlement);
			}
		}

		function acctlookupView(payDetail) {
			modalService.openCustomDialogs({
				"title" : "title.popup.view.custaccount",
				"dialogsModule" : "cda.custacct.popupview",
				"params" : {
					"custAcctId" : payDetail.custAcctId
				},
				"btnCallback" : {
					"ok" : function() {
					}
				}
			});
		}

		function splitPayDetail(settlement, payDetail) {
			for (var i = 0; i < settlement.setlDetails.length; i++) {
				var setlDetail = settlement.setlDetails[i];
				if (setlDetail.paydetailSeq == payDetail.payItemSeq) {
					var payDetail = payDetailService.addPayDetailRow(settlement, setlDetail, false, false);
					setlDetail.paydetailSeq = payDetail.payItemSeq;
					payDetailService.changePaydetailBySetldetail(settlement, setlDetail);
				}
			}
		}

		/**
		 * init companycode for paydetail loop up customer account.
		 */
		function setPaydetailCompanyCode(settlement) {
			angular.forEach(settlement.payDetails, function(data, index, array) {
				var alloc = data.allocCode;
				data.companyCode = settlementInitFactory.getFunction(settlement.settlementInfo, "getCompanyCodeByAlloc")(settlement, alloc);
				var arr = [];
				var parentType = settlement.settlementInfo.parentType;
				if (parentType == "REPAY") {
					var payTypeVal = settlement.payment.repayType;
					if (payTypeVal != null && payTypeVal == "C")
						arr = settlement.payCodeList;
				} else {
					if (data.itemSubtype == clsConstants.PayDetailItemSubType.TRANSFER_ITEM)
						arr = settlement.payCodeList;
					else
						arr = payDetailService.getSelectedPayCodeObjArr(settlement.payCodeList, data.payCcy);
				}
				var paycode = data.paycode;
				var crdr = data.crDrFlag;
				if (arr != null) {
					for (var i = 0; i < arr.length; i++) {
						if (arr[i].paycode == paycode && (arr[i].usedFor == crdr || arr[i].usedFor == "B")) {
							data.payCodeObj = data.payCodeObj || {};
							data.payCodeObj.paycodeCat = arr[i].paycodeCat;
							data.payCodeObj.paycode = arr[i].paycode;
							data.payCodeObj.feeCode = arr[i].feeCode;
							break;
						}
					}
				}
			});
		}

		return {
			paycodeChanged : paycodeChanged,
			acctlookupView : acctlookupView,
			splitPayDetail : splitPayDetail,
			setPaydetailCompanyCode : setPaydetailCompanyCode
		};
	}
})(angular);
